﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace LanAnh
{
    public partial class frmNguoiDungMoi : Office2007Form 
    {
        public frmNguoiDungMoi()
        {
            InitializeComponent();
        }
        Data data = new Data();
        string sql;
        string them = "";
        private void frmNguoiDungMoi_Load(object sender, EventArgs e)
        {
            btnRefresh_Click(sender, e);
            
        }

        private void GridViewX_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            txtHoTen.Text  = GridViewX.Rows[e.RowIndex].Cells["HoTen"].Value.ToString();
            txtTenDangNhap.Text = GridViewX.Rows[e.RowIndex].Cells["TenDangNhap"].Value.ToString();
            txtMatKhau.Text = GridViewX.Rows[e.RowIndex].Cells["MatKhau"].Value.ToString();
            cbPhanQuyen.Text  = GridViewX.Rows[e.RowIndex].Cells["Quyen"].Value.ToString();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            them = "them";
            txtHoTen.Text = "";
            txtTenDangNhap.Text = "";
            txtMatKhau.Text = "";
            txtHoTen.ReadOnly = false;
            txtTenDangNhap.ReadOnly = false;
            txtMatKhau.ReadOnly = false;
            cbPhanQuyen.Enabled = true;
            txtTenDangNhap.Focus();
            btnLuu.Enabled = true;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            cbPhanQuyen.Text = "Chỉ Xem";
            GridViewX.Enabled = false ;

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            them = "sua";
            txtTenDangNhap.Enabled = false;
            btnLuu.Enabled = true;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            GridViewX.Enabled = true;
            txtHoTen.ReadOnly = false;
            txtTenDangNhap.ReadOnly = false;
            txtMatKhau.ReadOnly = false;
            cbPhanQuyen.Enabled = true;

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Bạn có muốn xóa tài khoản '" + txtTenDangNhap.Text  + "'  này không ?", "DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                try
                {
                    sql = "Delete From tblDangNhap Where cTenDangNhap= N'" + txtTenDangNhap.Text  + "'";
                    data.executeNonQuery(sql);
                    btnRefresh_Click(sender, e);
                }
                catch
                {
                    MessageBoxEx.Show("Không thể xóa  !");
                }

            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            


            if (them == "them")
            {
                try
                {
                    if (txtTenDangNhap.Text.Replace("  ","") == "")
                    {
                        MessageBoxEx.Show("Nhập Tên Đăng Nhập !");
                        txtTenDangNhap.Focus();
                    }
                    else
                    {
                        
                        sql = "INSERT INTO tblDangNhap(cTenDangNhap,cMatKhau,cLoaiNguoiDung,cHoTen)VALUES(N'" + txtTenDangNhap.Text.ToLower().Replace(" ","") + "',N'" + getMd5Hash(txtMatKhau.Text) + "',N'" + cbPhanQuyen.Text + "',N'" + txtHoTen.Text + "')";
                        data.executeNonQuery(sql);
                        btnRefresh_Click(sender, e);
                        btnLuu.Enabled = false;                        
                    }


                }
                catch
                {
                    MessageBoxEx.Show("Tên đăng nhập này đã có !");
                    txtTenDangNhap.Focus();
                }
            }
            if (them == "sua")
            {
                sql = "UPDATE tblDangNhap SET cMatKhau=N'" + getMd5Hash(txtMatKhau.Text) + "',cLoaiNguoiDung=N'" + cbPhanQuyen.Text   + "',cHoTen=N'" + txtHoTen.Text  + "' WHERE cTenDangNhap=N'" + txtTenDangNhap.Text  + "'";
                data.executeNonQuery(sql);
                btnRefresh_Click(sender, e);
            }
            
            GridViewX.Enabled = true;
        }
        private string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        } 
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            txtTenDangNhap.Enabled = true;
            btnLuu.Enabled = false;
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            GridViewX.Enabled = true;
            txtHoTen.ReadOnly = true ;
            txtTenDangNhap.ReadOnly = true;
            txtMatKhau.ReadOnly = true;
            cbPhanQuyen.Enabled = false;
            sql = "SELECT * FROM tblDangNhap";
            GridViewX.DataSource = data.executeQueryTable(sql);
        }

    }
}
